
***********************************
*******UNDERSTANDING SOCIETY*******
***********************************
* EXTRACT PRECISE MONTH OF BIRTH

*YEAR AND MONTH OF BIRTH OF EACH INDIVIDUAL - US DATA
use "${US}\ukhls_wx\xwaveid_protect.dta", clear
keep pidp birthm birthy sex
sort pidp
tempfile dates
save "`dates'"

*YEAR AND MONTH OF BIRTH OF EACH INDIVIDUAL - BHPS DATA
use "${BHPS}\bhps_wx\xwaveid_bh_protect.dta", clear
keep pidp birthm birthy 	
append using "`dates'"
duplicates tag pidp, gen(dup)
bysort pidp (dup): keep if _n==1
duplicates report pidp
sort pidp
tempfile dates1
save "`dates1'"

***********************************************
local waves "a b c d e f g h i"

****PREPARE DIFFERENT WAVES FOR UKHLS DATA
local i = 1
local j=18
foreach wave of local waves {
	use "${US}\ukhls_w`i'\\`wave'_indresp_protect", clear
	renpfix `wave'_

	lookfor  vote  intdat  maedqf maju highest maid nnsib_dv ethn_dv urban_dv ///
	marstat_dv orgm  orga 	education school 

	keep pidp hidp pno  gor_dv   `r(varlist)'  employ ukborn single_dv qfhigh_dv 

	gen wave=`j'
	tempfile wave`i'
	save "`wave`i''"
	local ++i
	local ++j
	}

use "`wave1'", clear

local waves " b c d e f g h i "
local i = 1
foreach wave of local waves {
	local ++i
	append using "`wave`i''"
		}
		

*MERGE YEAR AND MOTNH OF BIRTH
sort pidp
merge pidp using "`dates'"
keep if _merge==3
drop _merge

* In education
gen 	in_education=(school==3) if school>0 // school
replace in_education=(fenow==3) if in_education!=1 & fenow>0
lab var in_education "Currently in education"


*HARMONISE WITH BHPS
ren gor_dv region
gen year=intdaty_dv		
gen intm=intdatm_dv		

gen bhps=0

*born abroad
gen born_abroad_temp=ukborn==5 if ukborn>0 & ukborn<.
bysort pidp (wave): egen born_abroad =max(born_abroad_temp)

*mothers education
gen mumlowtemp=maedqf <=2 if (maedqf>0 & maedqf<80)
bysort pidp (wave): egen mum_lowedu=max(mumlowtemp)

* Still in education
tab school, nol


* mothers ethnic background
gen mum_brittemp=(maid==1 | maid==2) if maid>0
bysort pidp (wave): egen mum_brit=max(mum_brittemp)
lab var mum_brit "Mother ethnic background: White Uk or Irish"

* mother worked
gen mwork14_tmp=maju==1 if maju>0 & maju<5
bysort pidp: egen mwork14=max(mwork14_tmp)
lab var mwork14 "Mother worked when aged 14"

* education (respondent)
gen low_edu=(hiqual_dv>=4) if hiqual_dv>0
lab var low_edu "Highest education GSCE or lower"

* siblings
ren nnsib_dv siblings

* marital status
gen married=(marstat_dv==1) if marstat>0
lab var married "Married or civil partner"

* respondent ethnic background
gen  white_brit=(ethn_dv==1 | ethn_dv==2) if ethn_dv>0
lab var white_brit "Ethnig background: White UK or Irish"
tab white_brit

* urban area
gen urban=(urban_dv==1) if urban_dv>0
lab var urban "Urban area"

* member of political party
gen member_party=(orgm1==1) if orgm1>=0 & orgm1<.
replace member_party=(orgmt1) if orgmt1>=0 & orgmt1<. & member_party==.
replace member_party=(orgmcawi1) if orgmcawi1>=0 & orgmcawi1<. & member_party==.
tab member_party

drop orgm1 orgm2 orgm3 orgm4 orgm5 orgm6 orgm7 orgm8 orgm9 orgm10 orgm11 orgm12 orgm13 orgm14 orgm15 orgm16 orgm96 orgmt1 orgmt2 orgmt3 orgmt4 orgmt5 orgmt6 orgmt7 orgmt8 orgmt9 orgmt10 orgmt11 orgmt12 orgmt13 orgmt14 orgmt15 orgmt16 orgmt96 orgmcawi1 orgmcawi2 orgmcawi3 orgmcawi4 orgmcawi5 orgmcawi6 orgmcawi7 orgmcawi8 orgmcawi9 orgmcawi10 orgmcawi11 orgmcawi12 orgmcawi13 orgmcawi14 orgmcawi15 orgmcawi16 orgmcawi96
drop *tmp *temp

tempfile us
save "`us'"


***************************************************************
**********************       BHPS DATA************************
****************************************************************
****PREPARE DIFFERENT WAVES
local i = 1
local year=1991
*START WITH ASCII CODE FOR A (97) UP TO R (114)

forvalues w=97(1)114{
*CONVERT NUMBER INTO LETTER - START WITH 'A'
local waves = char(`w')

foreach wave of local waves {
	use "${BHPS}\bhps_w`i'\b`wave'_indresp_protect", clear

	renpfix b`wave'_

	*GEN YEAR VARIABLE FOR WAVE 1
	if "`waves'"=="a"{
		gen year=1991
		}
	else{
	gen year=istrtdaty 
	}
	
	gen wave=`i'

	ren istrtdatd doid 
	ren istrtdatm doim
	
	*SPECIFY LIST OF VARIABLES TO KEEP
	lookfor vote  masoc90_cc maju maed jbhas plbornc qfedhi qfachi  bh_race bh_mlstat   size race orgm  orga 	 school type lprnt bg_school  bg_edtype_bh bg_fenow_bh

	keep pid hid pno wave doid doim year region `r(varlist)' /* hl*  age hhsize nchild mlstat mastat fiyr fihhmn jbstat  */ *race* *mlstat   
		
	tempfile wave`i'
	save "`wave`i''"
	
	local ++i
	local ++year
	}
}

*********************************
*YEAR AND MONTH OF BIRTH OF EACH INDIVIDUAL - BHPS DATA
use "${BHPS}\bhps_wx\xwaveid_bh_protect.dta", clear
keep pid pidp birthm birthy 	sex
sort pid
tempfile datesBHPS
save "`datesBHPS'"
*********************************
***********APPEND ALL WAVES
*********************************
use "`wave1'", clear

qui forvalues i=2(1)18 {
append using "`wave`i''"
}
recode year 92=1992 93=1993 94=1994 95=1995 96=1996 97=1997 

*MERGE YEAR AND MOTNH OF BIRTH
sort pid
merge pid using "`datesBHPS'"
tab _merge
keep if _merge==3
drop _merge

**SOME DATA PREP
gen bhps=1

* Still at school
gen still_school=(scnow==1) if scnow>0 & scnow<.
replace still_school=(school==2) if still_school==. & school>0
lab var still_school "Still at school"

*born abroad
gen born_abroad_temp=plbornc!=-8 if plbornc>=-8 & plbornc<.
bysort pid (wave): egen born_abroad =mode(born_abroad_temp)
drop born_abroad_temp
tab born_abroad  wave, col

*mothers education
gen mumlowtemp=maedqf <=2 if (maedqf>0 & maedqf<80)
bysort pidp (wave): egen mum_lowedu=max(mumlowtemp)
drop mumlowtemp
tab mum_lowedu wave, col

* mother worked 14
gen mwork14_tmp=0 if maju==2
replace mwork14_tmp=1 if maju==-8 & (masoc90_cc>0 & masoc90_cc<.)
bysort pidp: egen mwork14=max(mwork14_tmp)

* highest education (respondent)
gen low_edu=(qfedhi>=7) if qfedhi>0
lab var low_edu "Highest education GSCE or lower" 

* ethnic background
gen white_temp=(inrange(racel_b,1,4)) if racel_b>0 & racel_b<.
replace white_temp=(race==1) if (white_temp==. & race>0 & race<.)
bysort pidp: egen white_brit=max(white_temp)

* martial status
gen married=(mlstat==2 | mlstat==3) if mlstat>0
lab var married "Married or civil partnership"

gen member_party=(orgm1==1) if inrange(orgm1,0,1)
tab member_party

*********************************
***********APPEND BHPS WITH US **
*********************************
append using "`us'"

xtset pidp wave

save "Data_temp\bhps_US_vote_extract.dta", replace

